//
//  CommonFunc.m
//  GasCome
//
//  Created by zfc on 2020/4/29.
//  Copyright © 2020 zfc. All rights reserved.
//

#import "CommonFunc.h"
#import "BaseTabBarVC.h"
#import "BaseNavigationVC.h"
#import "LoginVC.h"

@implementation CommonFunc

+ (void) showLogin:(UIViewController * _Nullable) vc {

    if (vc == nil) {

        dispatch_async(dispatch_get_main_queue(),^{
            UIViewController *vc1 = [UIApplication.sharedApplication.keyWindow rootViewController];
            if ([vc1 isKindOfClass:[BaseTabBarVC class]]) {
                BaseTabBarVC *vc2 = (BaseTabBarVC *)vc1;
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"是否登录" preferredStyle:UIAlertControllerStyleAlert];
                // 确定注销
                UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
                    LoginVC *loginVC = [[LoginVC alloc]init];
                    BaseNavigationVC *navi = [[BaseNavigationVC alloc] initWithRootViewController:loginVC];
                    navi.modalPresentationStyle = UIModalPresentationFullScreen;
                    [vc2 presentViewController:navi animated:YES completion:nil];
                }];
                UIAlertAction *cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
                    //            exit(0);
                    vc2.selectedIndex = 0;

                }];

                [alert addAction:okAction];
                [alert addAction:cancelAction];

                // 弹出对话框
                [vc2 presentViewController:alert animated:true completion:nil];
            }
        });

        return;

    }

    if(!UserAccountManager.shared.is_login){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"是否登录" preferredStyle:UIAlertControllerStyleAlert];
        // 确定注销
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
            LoginVC *loginVC = [[LoginVC alloc]init];
            BaseNavigationVC *navi = [[BaseNavigationVC alloc] initWithRootViewController:loginVC];
            navi.modalPresentationStyle = UIModalPresentationFullScreen;

            [vc presentViewController:navi animated:YES completion:nil];
        }];
        UIAlertAction *cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
            //            exit(0);
            UIViewController *vc = [UIApplication.sharedApplication.keyWindow rootViewController];
            BaseTabBarVC *vc1 = (BaseTabBarVC *)vc;
            vc1.selectedIndex = 0;
        }];

        [alert addAction:okAction];
        [alert addAction:cancelAction];

        // 弹出对话框
        [vc presentViewController:alert animated:YES completion:nil];
    }

}

//获取商家列表
+ (void)getCompanyListWithSuccessBlock:(void (^)(NSDictionary *response))successBlock failureBlock:(void (^)(NSError *error))failureBlock {
    [[NetWorksTools shared] requestWithMethod:GET url:@"/api/merchants/v1/merchant/index" params:nil successBlock:^(NSDictionary * _Nonnull response) {
        if (successBlock) {
            successBlock(response);
        }
    } failureBlock:^(NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}

@end
